using System.Collections.Generic;
using System.IO;
using System.Linq;
using Olemas.Models;

namespace Olemas.DataModels
{
    public class ConspectModel
    {
        /// <summary>
        /// Gets the number of conspects with the given faculty id.
        /// </summary>
        /// <param name="facId">The Id of the faculty of the conspect displayed.</param>
        public static int GetNumberOfConspects(int facId)
        {
            var db = new OlemasDatabaseEntities();
            var total = (from conspect in db.Conspects
                         where (facId == 0 || conspect.Course.Faculty.Id == facId)
                         select conspect).Count();
            return total;
        }

        /// <summary>
        /// Gets the conspects with the given faculty id.
        /// </summary>
        /// <param name="facId">The Id of the faculty of the conspects displayed.</param>
        /// <param name="page">The current page</param>
        /// <param name="pageSize">The amount of conspects to display on one page.</param>
        /// <param name="ordering">What to order the conspects by.</param>
        public static IQueryable<Conspect> GetConspects(int facId, int page, int pageSize, string ordering)
        {
            var db = new OlemasDatabaseEntities();
            var conspects = db.Conspects.Where(c => facId == 0 || c.Course.Faculty_Id == facId);
            switch (ordering)
            {

                case "NewestFirst":
                    conspects = conspects.OrderByDescending(d => d.DateAdded);
                    break;

                case "OldestFirst":
                    conspects = conspects.OrderBy(d => d.DateAdded);
                    break;

                case "Course":
                    conspects = conspects.OrderBy(d => d.Course.Name);
                    break;

                case "Name":
                    conspects = conspects.OrderBy(d => d.Name);
                    break;

                default:
                    conspects = conspects.OrderByDescending(d => d.DateAdded);
                    break;
            }
            return conspects.Skip(pageSize * (page - 1)).Take(pageSize);
        }

        /// <summary>
        /// Gets the conspect with the given id.
        /// </summary>
        /// <param name="id">The Id of the conspect.</param>
        public static Conspect GetConspectById(int id)
        {
            var db = new OlemasDatabaseEntities();
            var conspect = db.Conspects.Find(id);
            return conspect;
        }

        /// <summary>
        /// Gets the conspect file with the given id.
        /// </summary>
        /// <param name="id">The Id of the conspect file.</param>
        public static Conspect_File GetConspectFileById(int id)
        {
            var db = new OlemasDatabaseEntities();
            var conspectFile = db.Conspect_Files.Find(id);
            return conspectFile;
        }

        /// <summary>
        /// Checks if the conspect file exists in the supplied location
        /// </summary>
        /// <param name="filePath">The path to the file to be checked.</param>
        public static bool CheckFile(string filePath)
        {
            return File.Exists(filePath);
        }
    }
}